$(function(){
    var userId = sessionStorage.getItem('userId');
    console.log(userId);
    var  pri = window.localStorage.getItem("prices");
    $(".prices").html(pri+"元");
    var url=window.location.href;
    var modelsend=decodeURI(getParamer('modelSend'));
        modelsend.dillName=pri;
    var model=jQuery.parseJSON(modelsend);
        model.dillName = pri;
    console.log(model);
    function fmoney(s, n)
    {
        n = n > 0 && n <= 20 ? n : 2;
        s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + '';
        var l = s.split('.') [0].split('').reverse(),
            r = s.split('.') [1];
        var  t = '';
        for (var i = 0; i < l.length; i++)
        {
            t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? ',' : '');
        }
        return t.split('').reverse().join('') ;
    }
    //tab切换
    function change(){
        var sku_num = {
            "model": 'sale_price',
            "condition": {"year_month":''},
            "item": {"sku_sum": [1,2],
                "main_index": {"sku_sum": 1}//主指标
            }//数据指标
        };
        $('.content').children('div').eq(0).show();
        $('.content').children('div').eq(1).hide();
        $('.content').children('ul').children('li').click(function(){
            sku_num.dillName=pri;
            console.log(sku_num)
            $('.cur').removeClass('cur');
            $(this).addClass('cur');
            var i=$(this).index();
            console.log(i)
            if(i==0){
                $.ajax({
                    url: "https://up-bi.pec.com.cn/data-center/month/getSaleData?token="+userId,
                    type: "POST",
                    data:{"paramData":JSON.stringify(model)},
                    success:function(data){
                        console.log(data);
                        var str="";
                        for (var i in data.data){
                            str+='<tr>';
                            if(data.data[i].price==null){
                                str+='<td style=""></td>';
                            }else{
                                str+='<td style="">'+data.data[i].price+'</td>';
                            }

                            str+='<td>'+fmoney(data.data[i].num,2)+'</td>' +
                                '<td>'+data.data[i].duty+'%'+'</td>' +
                                '</tr>'
                        };
                        $('#table tbody').html(str);
                    }
                });
                $('.content').children('div').eq(i).show().siblings('div').hide();
            }else{
                $.ajax({
                    url: "https://up-bi.pec.com.cn/data-center/month/getSaleData?token="+userId,
                    type: "POST",
                    data:{"paramData":JSON.stringify(sku_num)},
                    success:function(data){
                        console.log(data);
                        var str="";
                        for (var i in data.data){
                            str+='<tr>';
                            if(data.data[i].price==null){
                                str+='<td style=""></td>';
                            }else{
                                str+='<td style="">'+data.data[i].price+'</td>';
                            }

                            str+='<td>'+fmoney(data.data[i].num,2)+'</td>' +
                                '<td>'+data.data[i].duty+'%'+'</td>' +
                                '</tr>'
                        };
                        $('#tab tbody').html(str);
                    }
                });
                $('.content').children('div').eq(i).show().siblings('div').hide();
            }

        })
    };
    change();

    //接受传过来的参数,发送给后台，从后台接收数据，再次渲染
    $.ajax({
        url: "https://up-bi.pec.com.cn/data-center/month/getSaleData?token="+userId,
        type: "POST",
        data:{"paramData":JSON.stringify(model)},
        success:function(data){
            console.log(data);
            var str="";
            for (var i in data.data){
                str+='<tr>';
                if(data.data[i].price==null){
                    str+='<td style=""></td>';
                }else{
                    str+='<td style="">'+data.data[i].price+'</td>';
                }

                str+='<td>'+fmoney(data.data[i].num,2)+'</td>' +
                    '<td>'+data.data[i].duty+'%'+'</td>' +
                    '</tr>'
            };
            $('#table tbody').html(str);
        }
    });

    function getParamer(paramer){
        var url=window.location.href.split("?")[1];
        if(url.indexOf("&")>0){
            urlParamArry=url.split("&");
            for(var i=0; i<urlParamArry.length; i++){
                var paramerName=urlParamArry[i].split("=");
                if(paramer==paramerName[0]){
                    return paramerName[1];
                }
            }
        }else{
            var paramerValue=url.split("=")[1];
            return paramerValue;
        }
    };
})